package com.battle.project.order.service.impl;

import com.battle.project.order.mq.send.OrderSendMqHandler;
import com.battle.project.order.service.OrderInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    private static Logger logger = LoggerFactory.getLogger(OrderInfoServiceImpl.class);

    @Autowired
    private OrderSendMqHandler orderSendMqHandler;


    @Override
    public String findOrderInfoById() {
        return "order：" + 1;
    }


    @Value("${battle.order.fanout.exchange.name}")
    private String fanoutExchangeName;

    @Override
    public String fanoutSendMessage(String message) {
        orderSendMqHandler.sendMessage(fanoutExchangeName, null, message);
        return "消息发送成功";
    }

    @Value("${battle.order.direct.exchange.name}")
    private String directExchangeName;
    @Value("${battle.order.direct.routing.key}")
    private String directRoutingKey;

    @Override
    public String directSendMessage(String message) {
        orderSendMqHandler.sendMessage(directExchangeName, directRoutingKey, message);
        return "消息发送成功";
    }

    @Value("${battle.order.topic.exchange.name}")
    private String topicExchangeName;
    @Value("${battle.order.topic.routing.key}")
    private String topicRoutingKey;

    @Override
    public String topicSendMessage(String message) {
        orderSendMqHandler.sendMessage(topicExchangeName, topicRoutingKey, message);
        return "消息发送成功";
    }

    @Value("${battle.order.headers.exchange.name}")
    private String headersExchangeName;

    @Override
    public String headersSendMessage(String message) {
        Message mes = MessageBuilder.withBody(message.getBytes(StandardCharsets.UTF_8)).setHeader("age", "1").build();
        orderSendMqHandler.sendHeadersMessage(headersExchangeName, null, mes);
        return "消息发送成功";
    }
}
